﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Threading;
using System.Globalization;
using System.Data;
using SystemFrameWork;
using SystemFrameWork.Enums;
using Entity.Model;

namespace DutyWeb
{
    public class UserSession
    {
        #region public property

        /// <summary>
        /// Get the single object
        /// </summary>
        public static UserSession CurrentSession
        {
            get
            {
                return currentSession();
            }
        }

        #region Web Properties

      
        /// <summary>
        /// Get/Set the check code
        /// </summary>
        public string VerificationCode
        {
            get
            {
                return GetValue(ConstantsManager.VerificationCode).ToString();
            }

            set
            {
                SetValue(ConstantsManager.VerificationCode, value);
            }
        }

        /// <summary>
        /// Get/Set the current sys_user
        /// </summary>
        public Sys_User CurrentCandidateAccount
        {
            get
            {
                return GetValue(ConstantsManager.Sys_userkey) as Sys_User;
            }
            set
            {
                SetValue(ConstantsManager.Sys_userkey, value);
            }
        }

        /// <summary>
        /// Get/Set the CurrentPfizerUser
        /// </summary>
        /// add by nina 2010-01-29
        public string Username
        {
            get
            {
                return (string)GetValue(ConstantsManager.Username);
            }
            set
            {
                SetValue(ConstantsManager.Username, value);
            }
        }

        /// <summary>
        /// Get/Set the current role
        /// </summary>
        public List<Sys_Role> CurrentRole
        {
            get
            {
                return GetValue(ConstantsManager.CurrentRole) as List<Sys_Role>;
            }

            set
            {
                SetValue(ConstantsManager.CurrentRole, value);
            }
        }


        /// <summary>
        /// Get/Set the current user group
        /// </summary>
        /// add by nina 2010-01-28
        public bool EnableDataPermission
        {
            get
            {
                return (bool)GetValue(ConstantsManager.EnableDataPermission);
            }

            set
            {
                SetValue(ConstantsManager.EnableDataPermission, value);
            }
        }

        public DataTable ErrorImportMessage
        {
            get
            {
                return GetValue(ConstantsManager.ErrorImportMessage) as DataTable;
            }
            set
            {
                SetValue(ConstantsManager.ErrorImportMessage, value);
            }
        }

        /// <summary>
        /// Removed ExampaperID Of exam Definition
        /// </summary>
        public List<Guid> RemovedExampaperIDOfDefinition
        {
            get
            {
                return GetValue(ConstantsManager.RemovedExampaperOfDefinition) as List<Guid>;
            }
            set
            {
                SetValue(ConstantsManager.RemovedExampaperOfDefinition, value);
            }
        }

        /// <summary>
        /// place the question types into a sessoin, which will enable
        /// other methods to look up
        /// </summary>
        public Dictionary<Guid, string> QuestionTypeDictionary
        {
            get
            {
                return GetValue(ConstantsManager.QuestionTypeDictionary) as Dictionary<Guid, string>;
            }
            set
            {
                SetValue(ConstantsManager.QuestionTypeDictionary, value);
            }
        }

        public DataView DataViewEmployee
        {
            get
            {
                return GetValue(ConstantsManager.DataViewEmployee) as DataView;
            }
            set
            {
                SetValue(ConstantsManager.DataViewEmployee, value);
            }
        }


        /// <summary>
        /// the temp file url after upload
        /// </summary>
        public string TempFileUrl
        {
            get
            {
                return GetValue(ConstantsManager.TempFileUrl) as string;
            }
            set
            {
                SetValue(ConstantsManager.TempFileUrl, value);
            }
        }

        #endregion

        #endregion

        #region Private Fields

        private static UserSession _instance;

        #endregion

        #region Static Methods

        /// <summary>
        /// confine the UserSession to a singleton type
        /// </summary>
        private UserSession()
        { }

        /// <summary>
        /// Create object follow singleton pattern
        /// </summary>
        /// <returns></returns>
        private static UserSession currentSession()
        {
            if (_instance == null)
            {
                _instance = new UserSession();
            }
            return _instance;
        }

        #endregion

        #region Private Methods

        private object GetValue(string key)
        {
            if (HttpContext.Current.Session == null)
            {
                return null;  //LogHelper.Log(LogLevel.Info, ResourceManager.GetString(ResourceManager.Message_InvalidRequestKey));
                //throw new BaseException(ResourceManager.GetString(ResourceManager.Message_InvalidRequestKey));
            }
            try
            {
                if (HttpContext.Current.Session[key] != null)
                {
                    return HttpContext.Current.Session[key];
                }
                else
                {
                    ///HttpContext.Current.Response.Redirect("../Login/PfizerStaffLogin.aspx");
                    return null;
                }
            }
            catch (Exception ex)
            {
                throw new BaseException(ex.Message);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="key"></param>
        /// <param name="value"></param>
        private void SetValue(string key, object value)
        {
            try
            {
                HttpContext.Current.Session[key] = value;
            }
            catch (Exception ex)
            {
                throw new BaseException(ex.Message);
            }
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Clear all session variables
        /// </summary>
        public void Clear()
        {
            HttpContext.Current.Session.Clear();
        }

        /// <summary>
        /// Abandon the session
        /// </summary>
        public void Abandon()
        {
            HttpContext.Current.Session.Abandon();
        }

        /// <summary>
        /// Reset session
        /// </summary>
        public void Reset(string key)
        {
            SetValue(key, null);
        }

        #endregion
    }
}
